//
//  BaseViewController+Style.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/2.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "BaseViewController+Style.h"
#import "NSTimer+Blocks.h"
#import "SignInputViewController.h"
#import "BaseNavigationController.h"
@implementation BaseViewController (Style)
NSTimer *timer;
float scanWidth;
float scanHeight;
-(void)popBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 扫一扫
-(void)showQzStyle:(NSInteger)style
{
    scanHeight = 130.00*(SCREEN_HEIGHT/480.00f);
    scanWidth = 50*(SCREEN_WIDTH/320.00f);
    readerVC = [ZBarReaderViewController new];
    readerVC.readerDelegate = self;
    readerVC.supportedOrientationsMask = ZBarOrientationMaskAll;
    readerVC.showsCameraControls=NO;
    readerVC.showsZBarControls=NO;
    readerVC.cameraFlashMode=UIImagePickerControllerCameraFlashModeOff;
    readerVC.cameraMode=ZBarReaderControllerCameraModeSampling;
    readerVC.cameraOverlayView=[self camerOverLayView:style];
    
    

//    float width  =
    
    readerVC.scanCrop = CGRectMake(scanWidth/SCREEN_WIDTH, scanHeight/SCREEN_HEIGHT,(SCREEN_WIDTH-scanWidth*2)/SCREEN_HEIGHT,(SCREEN_WIDTH-scanWidth*2)/SCREEN_HEIGHT);
    
//    UIView *testView = [[UIView alloc]initWithFrame:CGRectMake(scanWidth, scanHeight, SCREEN_WIDTH-scanWidth*2, SCREEN_WIDTH-scanWidth*2)];
//    testView.backgroundColor = [UIColor yellowColor];
//    testView.alpha = 0.3;
//    [readerVC.view addSubview:testView];
    
    
    ZBarImageScanner *scanner = readerVC.scanner;
    // TODO: (optional) additional reader configuration here
    // EXAMPLE: disable rarely used I2/5 to improve performance
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_POSITION
                       to: 0];
    
    // present and release the controller
    
    [self presentViewController:readerVC animated:YES completion:nil];
}
#pragma mark 从相册中找
-(void)showQzFromLibraryStyle
{
    
//    dispatch_async(dispatch_get_main_queue(), ^{
    [self exitReader];
//    });
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        zbarRC = [ZBarReaderController new];
//
        zbarRC.allowsEditing = NO   ;
        
        zbarRC.showsHelpOnFail = NO;
        zbarRC.readerDelegate = self;
        ZBarImageScanner *scanner = readerVC.scanner;
        // TODO: (optional) additional reader configuration here
        // EXAMPLE: disable rarely used I2/5 to improve performance
        [scanner setSymbology: ZBAR_I25
                       config: ZBAR_CFG_POSITION
                           to: 0];
    
        zbarRC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:zbarRC animated:YES completion:nil];
//    });
    
}
#pragma mark 回调扫描二维码结果
-(void)getZBarPushBlockStrStyle:(ZbarPushBlock)block
{
    
    self.zBlock = block;
}
- (void) imagePickerController: (id) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
    {
        [reader dismissViewControllerAnimated:YES completion:nil];
        NSString *qRUrl  = symbol.data;
        self.zBlock(qRUrl);
        break;
    }
    
    //    }
}

-(UIView *)camerOverLayView:(NSInteger)style
{
    UIView *view=[[UIView alloc] init];
    
    [view setFrame: [UIScreen mainScreen].bounds];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:view.frame];
    imageView.image = [UIImage imageNamed:@"pick_bg"];
    [view addSubview:imageView];
    
    //退出按钮
    UIButton *exit=[[UIButton alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT-44, SCREEN_WIDTH, 44)];
    [exit setTitle:@"退出" forState:0];
    [exit addTarget:self action:@selector(exitReader) forControlEvents:64];
    [view addSubview:exit];
    
    
    //更多按钮
    UIButton *moreBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-70, 30, 40, 25)];
    [moreBtn setTitle:@"更多" forState:0];
    moreBtn.tag = style+10;
    [moreBtn addTarget:self action:@selector(moreOperation:) forControlEvents:64];
    [view addSubview:moreBtn];
    
    
    __block BOOL  upOrdown = NO;
    __block int   num =0;
    __block UIImageView  *_line = [[UIImageView alloc] initWithFrame:CGRectMake(50, 120, SCREEN_WIDTH-100, 2)];
    _line.image = [UIImage imageNamed:@"line.png"];
    [view addSubview:_line];
    UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(15, 40, SCREEN_WIDTH-30, 50)];
    labIntroudction.backgroundColor = [UIColor clearColor];
    labIntroudction.numberOfLines=2;
    labIntroudction.textColor=[UIColor whiteColor];
    labIntroudction.text=NSLocalizedString(@"扫描二维码", nil);
    [view addSubview:labIntroudction];
    
    timer= [NSTimer scheduledTimerWithTimeInterval:0.02f block:^{
        if (upOrdown == NO) {
            num ++;
            _line.frame = CGRectMake(scanWidth, scanHeight+2*num, SCREEN_WIDTH-scanWidth*2, 2);
            if (2*num>=SCREEN_WIDTH-scanWidth*2) {
                upOrdown = YES;
            }
        }
        else {
            num --;
            _line.frame = CGRectMake(scanWidth, scanHeight+2*num, SCREEN_WIDTH-scanWidth*2, 2);
            if (num == 0) {
                upOrdown = NO;
            }
        }
    } repeats:YES];
    return view;
    
}
-(void)moreOperation:(UIButton *)sender
{
    NSArray *menuItems =nil;
    switch (sender.tag-10) {
        case SignZbarStyle:
        {
            menuItems =
            @[
              
              [KxMenuItem menuItem:@"从相册中选取二维码图片"
                             image:nil
                            target:self
                            action:@selector(showQzFromLibraryStyle)],
              
              [KxMenuItem menuItem:@"输入机构代码"
                             image:nil
                            target:self
                            action:@selector(pushMenuItem)],
              ];
        }
        break;
        case MyZbarStyle:
            menuItems =
            @[
              
              [KxMenuItem menuItem:@"从相册中选取二维码图片"
                             image:nil
                            target:self
                            action:@selector(showQzFromLibraryStyle)],
              
              [KxMenuItem menuItem:@"输入志愿者号/组织号"
                             image:nil
                            target:self
                            action:@selector(pushMenuItemFromMyView)],
              ];
        break;
            case ActivityStyle:
            menuItems =
            @[
              
              [KxMenuItem menuItem:@"从相册中选取二维码图片"
                             image:nil
                            target:self
                            action:@selector(showQzFromLibraryStyle)],
              
              [KxMenuItem menuItem:@"输入志愿者号/组织号"
                             image:nil
                            target:self
                            action:@selector(pushMenuItemFromMyView)],
              ];
            break;
    }
    
    
    //    readVC.cameraOverlayView
    if (menuItems) {
        [KxMenu showMenuInView:readerVC.cameraOverlayView
                      fromRect:sender.frame
                     menuItems:menuItems];
    }
    
}
#pragma mark 输入机构
-(void)pushMenuItem
{
    [self exitReader];
    SignInputViewController *inputVC = [[SignInputViewController alloc]initWithNibName:@"SignInputViewController" bundle:nil];
    [inputVC setIsPushSignVC:YES];
    BaseNavigationController *baseNV = [[BaseNavigationController alloc]initWithRootViewController:inputVC];
    [self presentViewController:baseNV animated:YES completion:nil];
    
}
#pragma mark 从个人简介那里跳进
-(void)pushMenuItemFromMyView
{
    [self exitReader];
    SignInputViewController *inputVC = [[SignInputViewController alloc]initWithNibName:@"SignInputViewController" bundle:nil];
    [inputVC setIsPushSignVC:NO];
    BaseNavigationController *baseNV = [[BaseNavigationController alloc]initWithRootViewController:inputVC];
    [self presentViewController:baseNV animated:YES completion:nil];
}
-(void)exitReader{
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
    [readerVC dismissViewControllerAnimated:NO completion:nil];
}

- (void) readerControllerDidFailToRead: (ZBarReaderController*) reader
                             withRetry: (BOOL) retry{
    if (timer) {
        [timer invalidate];
        timer = nil;
    }
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"不能识别" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [reader dismissViewControllerAnimated:YES completion:nil];
}
-(void)dealloc
{
    readerVC = nil;
    readerVC.readerDelegate = nil;
    zbarRC = nil;
    zbarRC.readerDelegate = nil;
}
@end
